package cn.edu.sgu.www.mhxysy.controller;

import cn.edu.sgu.www.mhxysy.consts.MimeType;
import cn.edu.sgu.www.mhxysy.dto.AccountDTO;
import cn.edu.sgu.www.mhxysy.entity.Account;
import cn.edu.sgu.www.mhxysy.pager.AccountPager;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.AccountService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author heyunlin
 * @version 1.0
 */
@RestController
@RequestMapping(path = "/account", produces = MimeType.APPLICATION_JSON_CHARSET_UTF_8)
public class AccountController {

	private final AccountService accountService;

	@Autowired
	public AccountController(AccountService accountService) {
		this.accountService = accountService;
	}

	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public JsonResult<Void> insert(@Validated AccountDTO accountDTO) {
		accountService.insert(accountDTO);

		return JsonResult.success("添加成功");
	}

	@RequestMapping(value = "/deleteById", method = RequestMethod.POST)
	public JsonResult<Void> deleteById(@RequestParam String id) {
		accountService.deleteById(id);

		return JsonResult.success("删除成功");
	}

	@RequestMapping(value = "/updateById", method = RequestMethod.POST)
	public JsonResult<Void> updateById(@Validated AccountDTO accountDTO) {
		accountService.updateById(accountDTO);

		return JsonResult.success("修改成功");
	}

	@RequestMapping(value = "/selectAll", method = RequestMethod.GET)
	public JsonResult<List<Account>> selectAll() {
		List<Account> list = accountService.selectAll();

		return JsonResult.success(null, list);
	}

	@RequestMapping(value = "/selectById", method = RequestMethod.GET)
	public JsonResult<Account> selectById(@RequestParam String id) {
		Account account = accountService.selectById(id);

		return JsonResult.success(null, account);
	}

	@RequestMapping(value = "/selectByPage", method = RequestMethod.POST)
	public JsonResult<JsonPage<Account>> selectByPage(AccountPager pager) {
		Page<Account> result = accountService.selectByPage(pager);

		return JsonResult.restPage(result);
	}

}